package com.qzsoft.zhjd.cement.modules.cementalarm.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.zhjd.cement.modules.cementalarm.dto.CementAlarmHandlerDto;
import com.qzsoft.zhjd.cement.modules.cementalarm.dto.CementAlarmListDto;
import com.qzsoft.zhjd.cement.modules.cementalarm.entity.CementAlarmEntity;
import com.qzsoft.zhjd.cement.modules.cementalarm.mapper.CementAlarmMapper;
import com.qzsoft.zhjd.cement.modules.cementalarm.service.CementAlarmService;
import com.qzsoft.zhjd.common.annotation.ZhjdDataFilter;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;

/**
 * @ClassName CementAlarmServiceImpl
 * @Description 预警信息业务接口实现
 * @author linhuawei
 * @since 2022-04-29
 */
@Service
public class CementAlarmServiceImpl implements CementAlarmService {

    @Autowired
    private CementAlarmMapper cementAlarmMapper;

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public IPage<CementAlarmListDto> page(Map<String, Object> params) {
        IPage<CementAlarmListDto> page = baseServiceComponent.getPage(params, CementAlarmListDto.class);
        return cementAlarmMapper.queryDto(page, params);
    }

    @Override
    public CementAlarmHandlerDto getHandlerById(String id) {
        CementAlarmEntity entity = cementAlarmMapper.selectById(id);
        CementAlarmHandlerDto dto = null;
        if (null != entity) {
            dto = new CementAlarmHandlerDto();
            dto.setId(id);
            dto.setAlarmReason(entity.getAlarmReason());
            dto.setHandlerPerson(entity.getHandlerPerson());
            dto.setHandlerPlan(entity.getHandlerPlan());
            dto.setHandlerResult(entity.getHandlerResult());
            dto.setHandlerTime(entity.getHandlerTime());
        }
        return dto;
    }

    @Override
    public void handler(CementAlarmHandlerDto dto) {
        UpdateWrapper<CementAlarmEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("alarm_reason", dto.getAlarmReason());
        updateWrapper.set("handler_plan", dto.getHandlerPlan());
        updateWrapper.set("handler_result", dto.getHandlerResult());
        updateWrapper.set("handler_person", dto.getHandlerPerson());
        updateWrapper.set("handler_time", dto.getHandlerTime());
        updateWrapper.set("state", 1);
        updateWrapper.eq("id", dto.getId());
        cementAlarmMapper.update(null, updateWrapper);
    }

}
